<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/3/6
 * Time: 下午4:15
 */

namespace Biz\service;

use Biz\model\trade\Cart as mCart;
use Biz\pockets\constInterface\ActivityInfo;
use Biz\service\activity\FullShipping as sFullShipping;
use Biz\service\activity\MealInfo as sMealInfo;
use Biz\tools\Amount;
/**
 * @deprecated
 */
class TipInfo
{
    public static function skuInCart(&$skuListInCart = [])
    {
        /**
         * TODO 显示排序
         */
        //限购提示
        //降价提示
        //底库存提示

        if (!is_null($skuListInCart)) {
            array_walk($skuListInCart, function (&$cartSku) {

                //住友津贴活动预热中
                if (!isset($cartSku['sku_tip'])) {
                    if($cartSku['activity_type'] == ActivityInfo::ACTIVITY_TYPE_ZHUYOUJINTIE && strtotime($cartSku['activity_start_time']) > time())
                        $cartSku['sku_tip'] = [
                            'icon' => '',
                            'text' => sprintf('将于'.$cartSku['activity_start_time'].'可购买')
                        ];
                }

                //限购提示
                if (!isset($cartSku['sku_tip'])) {
                    if (isset($cartSku['promo_info'])) {
                        $cartSku['sku_tip'] = [
                            'icon' => '',
                            'text' => sprintf('限购%s件', $cartSku['promo_info']['promo_limit']),
                        ];
                    }
                }

                //低库存提示
                if (!isset($cartSku['sku_tip'])) {
                    if ($cartSku['stock'] <= mCart::SKU_STOCK_MIN_NUM) {
                        $cartSku['sku_tip'] = [
                            'icon' => '',
                            'text' => '库存紧张'
                        ];
                    }
                }

                //降价提示(4.6.1去掉)
                /*if (!isset($cartSku['sku_tip'])) {
                    if ($cartSku['buy_price'] < $cartSku['o_buy_price']) {
                        $cartSku['sku_tip'] = [
                            'icon' => '',
                            'text' => sprintf('商品已降价¥%s', Amount::format($cartSku['o_buy_price'] - $cartSku['buy_price']))
                        ];
                    }
                }*/

            });
        }


    }

    public static function shopInCart(&$shopListInCart = [], $areaCode = '', $isSelect = false)
    {
        if ($shopListInCart) {
            //显示满包邮活动的提示信息,省份ID为空则不显示该信息
            if ($areaCode !== '') {

                $listData = [];

                array_map(function ($shopData) use ($isSelect, &$listData) {

                    if (!empty($shopData['sku_list'])) {

                        foreach ($shopData['sku_list'] as $cartSku) {

                            if (!isset($listData[$shopData['shop_info']['shop_id']])) {
                                $listData[$shopData['shop_info']['shop_id']][$cartSku['goods_id']] = [];
                            }

                            if ($isSelect) {
                                $listData[$shopData['shop_info']['shop_id']][$cartSku['goods_id']]['goods_amount'] += bcmul($cartSku['buy_price'], $cartSku['buy_num'], 2);
                                $listData[$shopData['shop_info']['shop_id']][$cartSku['goods_id']]['buy_num'] += $cartSku['buy_num'];
                            }
                        }
                    }

                }, $shopListInCart);


                //商家层面的满包邮tip
                $gapFeeList = sFullShipping::calGapFeeInfoInCart($areaCode, $listData);

                if (!empty($gapFeeList)) {

                    foreach ($shopListInCart as $shopId => &$shopData) {

                        if (($isSelect && $shopData['summary']['shipping_price'] > 0) || !$isSelect) {

                            if (isset($gapFeeList[$shopId])) {

                                $gapFeeInfo = '已参与满包邮';
                                if ($gapFeeList[$shopId]['gap_fee'] > 0) {
                                    if (!$gapFeeList[$shopId]['all_goods'] && !$isSelect) {
                                        $gapFeeInfo = sprintf('满包邮，部分商品满' . $gapFeeList[$shopId]['gap_fee'] . '元包邮');
                                    } else {
                                        $gapFeeInfo = sprintf('满包邮，' . (!$isSelect ? '满' : '还差') . $gapFeeList[$shopId]['gap_fee'] . '元即可包邮');
                                    }
                                }
                                if ($gapFeeList[$shopId]['gap_pieces'] > 0) {
                                    if (!$gapFeeList[$shopId]['all_goods'] && !$isSelect) {
                                        $gapFeeInfo = sprintf('满包邮，部分商品满' . $gapFeeList[$shopId]['gap_pieces'] . '件包邮');
                                    } else {
                                        $gapFeeInfo = sprintf('满包邮，' . (!$isSelect ? '满' : '还差') . $gapFeeList[$shopId]['gap_pieces'] . '件即可包邮');
                                    }
                                }

                                $shopData['shop_tips'][] = $gapFeeInfo;
                            }
                        }

                    }


                }

            }
        }

    }

    //套餐优惠信息显示

    public static function mealTip(&$skuListInCart = [], $isSelect = false)
    {
        if (!empty($skuListInCart)) {

            $listData = [];

            array_map(function ($skuData) use ($isSelect, &$listData) {
                if (!empty($skuData['goods_id'])) {

                    $listData[] = $skuData['goods_id'];

                }

            }, $skuListInCart);

            if (!empty($listData)) {
                $listData = array_unique($listData);
            }

            $mealInfoList = sMealInfo::getMealInfoByGoodsIds($listData);

            if (!empty($mealInfoList)) {
                foreach ($skuListInCart as &$skuData) {

                    $data = isset($mealInfoList[$skuData['goods_id']]) ? $mealInfoList[$skuData['goods_id']] : null;

                    if (!empty($data)) {
                        $data['cover_img'] = "http://img.hhz1.cn/Op-imageShow/5e4af304x01000000pem6g8?iv=1";
                    }

                    $skuData['meal_tip'] = $data;


                }
            }

        }
    }

    //收货地址限制提示

    public static function areaTip(&$skuListInCart = [], $areaCode)
    {
        if (!empty($skuListInCart)) {
            $listData = [];
            array_map(function ($skuData) use (&$listData) {
                if (!empty($skuData['goods_id'])) {
                    $listData[$skuData['goods_id']]['shipping_templ_id'] = $skuData['shipping_templ_id'];
                }
            }, $skuListInCart);

            $result = ShippingTempl::checkGoodsSupportShipping($listData, $areaCode);
            $nonsupport = isset($result['nonsupport_goods']) ? $result['nonsupport_goods'] : [];
            $supportAreas = $result['support_area'];
            if (!empty($nonsupport)) {
                foreach ($skuListInCart as &$skuData) {
                    $tip = null;
                    if (in_array($skuData['goods_id'], $nonsupport)) {
                        $supportArea = $supportAreas[$skuData['goods_id']];
                        $skuData['area_tip'] = [
                            'icon' => '',
                            'text' => '由于「the Paragraph 节选」仍在试运营阶段，该商品收货地址仅限' . $supportArea . '，其他地区将逐渐开放，敬请谅解'
                        ];

                        //3.14新增sku_tips 无法配送优先级高 有这个就不展示套餐
                        $skuTips = [];
                        array_push($skuTips, [
                            'icon' => \Biz\module\Classes\Order::SKU_TIP_ICON,
                            'text' => '当前地区无法配送',
                        ]);
                        $skuData['sku_tips'] = $skuTips;
                        //去掉套餐提示
                        unset($skuData['meal_tip']);
                    }
                }
            }

        }
    }

    //团购提示
    public static function groupTip(&$skuListInCart = [], $groupMap)
    {
        if (!empty($skuListInCart)) {
            //查询团购价格
            foreach ($skuListInCart as &$skuData) {
                $goodsId = $skuData['goods_id'];
                $skuId = $skuData['sku_id'];
                if (isset($groupMap[$goodsId])) {
                    $icon = isset($groupMap[$goodsId]['cover_img_url']) ? $groupMap[$goodsId]['cover_img_url'] : '';
                    $groupSkus = isset($groupMap[$goodsId]['skus']) ? $groupMap[$goodsId]['skus'] : [];
                    foreach ($groupSkus as $sku) {
                        if (($sku['sku_id'] == $skuId) && ($sku['actual_stock'] > 0)) {
                            $skuData['group_tip'] = [
                                'icon' => $icon,
                                'text' => vsprintf('拼团价仅需¥%.2f', [$sku['activity_price']])
                            ];
                            break;
                        }
                    }
                }

            }
        }
    }
}